/**
 * Copyright (c) 2010-2011 julewa.com.  
 * All rights reserved. 
 * 
 * @author Huang Benhua
 * @date 2011-7-20
 * 
**/
package com.julewa.db;

import java.awt.List;
import java.util.Set;

public enum PropertyType {
	PRIMARY(0){
		protected boolean _check_(Class<?> clazz){return clazz.isPrimitive();}
	},
	OBJECT(1){
		protected boolean _check_(Class<?> clazz){return false;}
	},
	ARRAY(11){
		protected boolean _check_(Class<?> clazz){return clazz.isArray();}
	},
	LIST(12){
		protected boolean _check_(Class<?> clazz){return List.class.isAssignableFrom(clazz);}
	},
	SET(13){
		protected boolean _check_(Class<?> clazz){return Set.class.isAssignableFrom(clazz);}
	};
	private int value = 0;
	private String label = null;
	private char brief;
	PropertyType(int v){
		value = v;
		label = name().toLowerCase();
		brief = label.charAt(0);
	}
	public String getLabel(){
		return label;
	}
	public int getValue(){
		return value;
	}
	public char getBrief(){
		return brief;
	}
	public boolean isCollectionType(){
		return value > 10;
	}
	protected abstract boolean _check_(Class<?> clazz);
	public static final PropertyType of(int value){
		for(PropertyType pt: PropertyType.values()){
			if(pt.value == value)return pt;
		}
		return null;
	}
	public static final PropertyType of(char c){
		c = Character.toLowerCase(c);
		for(PropertyType pt: PropertyType.values()){
			if(c == pt.brief)return pt;
		}
		return null;
	}
	public static final PropertyType of(String type){
		if(type == null || type.length() < 1)return null;
		return of(type.charAt(0));
	}
	public static final PropertyType of(Class<?> clazz){
		for(PropertyType pt: PropertyType.values()){
			if(pt._check_(clazz))return pt;
		}
		return OBJECT;
	}
}
